package edu.szu.huhanpeng.doodlingFight.view 
{
	import edu.szu.huhanpeng.doodlingFight.AppFacade;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IDisplayData;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class StageMed extends Mediator implements IMediator, INotifier 
	{
		public static const NAME:String = "stage";
		
		public function StageMed(stage:Stage) 
		{
			super( NAME, stage );
		}
		
		protected function get stage():Stage
		{
			return viewComponent as Stage;
		}
		
		//override public function listNotificationInterests():Array 
		//{
			//return [GlobalConst.noteName.STAGE_ADD_CHILD,
					//GlobalConst.noteName.STAGE_REMOVE_CHILD];
		//}
		
		override public function handleNotification(note:INotification):void 
		{
			super.handleNotification( note );
			
			var childName:String;
			switch ( note.getName() ) 
			{
				case GlobalConst.noteName.STAGE_ADD_CHILD:
					childName = note.getBody() as String;
					addChild( childName );
				break;
				
				case GlobalConst.noteName.STAGE_REMOVE_CHILD:
					childName = note.getBody() as String;
					removeChild(childName);
				default:
			}
		}
		
		private function removeChild(childName:String):void 
		{
			var child:DisplayObject = facade.retrieveMediator( childName ).getViewComponent() as DisplayObject;
			if (child.stage != null) 
			{
				stage.removeChild(child);
			}
		}
		
		private function addChild(childName:String):void 
		{
			var child:DisplayObject = facade.retrieveMediator( childName ).getViewComponent() as DisplayObject;
			var displayData:IDisplayData = facade.retrieveProxy( childName ).getData() as IDisplayData;
			child.x = displayData.x;
			child.y = displayData.y;
			if (child.stage == null) 
			{
				stage.addChild( child );
			}
			
		}
	}

}